package com.cino.demos.sourceCode.nginxDemo;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class SimpleLoadBalancer {
    private final List<String> servers;
    private final AtomicInteger currentIndex = new AtomicInteger(0);

    public SimpleLoadBalancer(List<String> servers) {
        this.servers = servers;
    }

    public String getNextServer() {
        int index = currentIndex.getAndIncrement() % servers.size();
        return servers.get(index);
    }

    public static void main(String[] args) {
        // 定义服务器列表
        List<String> servers = Arrays.asList(
            "http://server1:8080",
            "http://server2:8080",
            "http://server3:8080"
        );
        
        SimpleLoadBalancer balancer = new SimpleLoadBalancer(servers);
        
        // 模拟10次请求分发
        for (int i = 0; i < 10; i++) {
            System.out.println("Request " + (i+1) + " -> " + balancer.getNextServer());
        }
    }
}